package com.zixuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zixuan.common.Result;
import com.zixuan.common.dto.DeviceAndPurDTO;
import com.zixuan.common.utils.Constant;
import com.zixuan.dao.DeviceDao;
import com.zixuan.dao.PurApplicationDao;
import com.zixuan.entity.DeviceMannerEntity;
import com.zixuan.entity.PurApplicationEntity;
import com.zixuan.entity.PurchaseApplicationEntity;
import com.zixuan.service.DeviceService;
import com.zixuan.service.PurDeviceService;
import com.zixuan.service.PurchaseApplicationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PurDeviceServiceImpl implements PurDeviceService {
    @Autowired
    private PurApplicationDao purApplicationDao;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private PurchaseApplicationService purchaseApplicationService;

    @Override
    public Result purDevice(PurApplicationEntity purApplicationEntity) {
        int insert = purApplicationDao.insert(purApplicationEntity);
        return insert > 0 ? Result.ok() : Result.err();
    }

    @Override
    public Result findAllPurApplication(String start, String end) {
        Page<PurApplicationEntity> page = new Page<>(Integer.parseInt(start), Integer.parseInt(end));
        Page<PurApplicationEntity> selectPage = purApplicationDao.selectPage(page, null);
        return new Result(200, true, "SUCCESS", selectPage);
    }

    //TODO:此接口待完善 有bug  如果当前采购申请同意了 即插入到purchase_application中准备发放资产 否则就删除此表中的申请记录
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result permittedApplication(DeviceAndPurDTO deviceAndPurDTO) {
        PurApplicationEntity purApplicationEntity = new PurApplicationEntity();
        PurchaseApplicationEntity entity = new PurchaseApplicationEntity();
        BeanUtils.copyProperties(deviceAndPurDTO, purApplicationEntity);
        BeanUtils.copyProperties(deviceAndPurDTO, entity);
        if (purApplicationEntity.getIsAgree() == 0) {
            //不同意
            //删除当前记录 并且将设备的状态修改为未借出  如果在采购表中有此记录也需要删除
            purApplicationDao.deleteById(purApplicationEntity.getPurId());
            //查询
            PurchaseApplicationEntity purChaseApplicationByPurId = purchaseApplicationService.findPurChaseApplicationByPurId(purApplicationEntity.getPurId());
            if (purChaseApplicationByPurId != null) {
                purchaseApplicationService.delPurChaseApplicationByInfoId(purChaseApplicationByPurId.getInfoId());
            }
            DeviceMannerEntity mannerEntity = new DeviceMannerEntity();
            mannerEntity.setDeviceStatus(Constant.DEVICE.NOTLOANED.getCode());
            mannerEntity.setDeviceId(purApplicationEntity.getDeviceId());
            deviceService.updateDevice(mannerEntity);
            return Result.ok();
        }
        //同意  即插入到purchase_application中准备发放资产
        purApplicationDao.updateById(purApplicationEntity);
        purchaseApplicationService.addPurChaseApplication(entity);
        return Result.ok();
    }

    @Override
    public Result findApplicationByUserId(String start, String end, String userId) {
        Page<PurApplicationEntity> page = new Page<>(Integer.parseInt(start), Integer.parseInt(end));
        Page<PurApplicationEntity> selectPage = purApplicationDao.selectPage(page, new QueryWrapper<PurApplicationEntity>().eq("em_id", userId));
        return new Result(200, true, "SUCCESS", selectPage);
    }


}
